
function subfields(str) {
    var res = {};
    var parts = str.split('^');
    var i;
    res['_'] = parts[0];
    for (i = 1; i < parts.length; i += 1) {
        res[parts[i].charAt(0)] = parts[i].slice(1);
    }
    return res;
}

var compositeField = function (value) {
    var separator = '^';
    var markers = [];
    return {
        parse : function (new_value) {
            var i, marker, chunks;
            if (typeof new_value === 'string') {
                value = new_value;
            }
            chunks = value.split(separator);
            this['_'] = chunks[0];
            markers.push('_');
            for (i = 1; i < chunks.length; ++i) {
                marker = chunks[i].charAt(0);
                this[marker] = chunks[i].slice(1);
                markers.push(marker);
            }
            return this;
        },
        dump : function () {
            var parts = {};
            for (i = 0; i < markers.length; ++i) {
                key = markers[i];
                if (this.hasOwnProperty(key)) {
                    parts[key] = this[key];
                }
            }
            return parts;
        }
    }
}
